import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { SshConnectionConfig } from '../../core/models/ssh-connection-config.model';
import { AppSettingService } from '../../core/services/app-setting.service';
import { CommonService } from '../../core/services/common.service';
import { MessageService } from '../../core/services/message.service';

@Component({
    selector: 'op-remote-setting',
    templateUrl: './remote-setting.component.html',
    styleUrls: ['./remote-setting.component.scss']
}

) export class RemoteSettingComponent implements OnInit {
    formGroup: FormGroup;
    private remoteSetting: SshConnectionConfig;

    constructor(
        private appSettingService: AppSettingService,
        private commonService: CommonService,
        private messageService: MessageService,
        private fb: FormBuilder
    ) { }

    ngOnInit(): void {
        this.remoteSetting = this.appSettingService.settings.remoteSetting;

        this.formGroup = this.fb.group({
            host: [this.remoteSetting.host, [Validators.required]],
            port: [this.remoteSetting.port, [Validators.required]],
            username: [this.remoteSetting.username, [Validators.required]],
            password: [this.remoteSetting.password, [Validators.required]]
        });
    }

    save(): void {
        this.commonService.validateForm(this.formGroup);

        if (this.formGroup.valid) {
            this.appSettingService.settings.remoteSetting = this.formGroup.value;
            this.appSettingService.save();
            this.messageService.showInfo('Setting saved');
        }
    }
}